package com.bstek.uflo.core.test;


import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

import com.bstek.uflo.env.EnvironmentProvider;

@Component
public class MyEnvironmentProvider implements EnvironmentProvider {
	public MyEnvironmentProvider() throws SQLException {
		System.out.println(MyEnvironmentProvider.class.getName()+" init...");
	}
	
	@Autowired
    private Environment env;
	
	public String getLoginUser() {
		return ContextHolder.getLoginUser();
	}

	@Override
	public String getCategoryId() {
		return ContextHolder.getCategoryId();
	}
	
	@Override
	public DataSource getDataSource() {
		env.getProperty("uflo.enableHisDataTransferJob");
//		appConfig.getUsername();
		SingleConnectionDataSource ds=new SingleConnectionDataSource(env.getProperty("url"), env.getProperty("user") , env.getProperty("password"), false);
		ds.setDriverClassName(env.getProperty("driverClassName"));
		return ds;
	}

	@Override
	public PlatformTransactionManager getPlatformTransactionManager() {
		PlatformTransactionManager ptm=new DataSourceTransactionManager(getDataSource());
		return ptm;
	}
	
}